package com.beijing.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author Ma Wenqi
 * @Date 2021/3/29 14:59
 * @Version 1.0
 */
public class Demo07 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c1 = Class.forName("com.beijing.reflection.User");
        //获得包名+类名
        User user = new User();
        Class c2 = user.getClass();
        //获得类的信息
        System.out.println(c1.getName());//获得包名+类名
        System.out.println(c1.getSimpleName());//获得类名
        //获得类的属性
        System.out.println("=======================");
        Field[] fields = c1.getFields();//获取类的公开属性和父类的公开属性
        fields = c1.getDeclaredFields();//获取类的任何属性
        for (Field field : fields) {
            System.out.println(field);
        }
        //获得指定属性的值
        Field name = c1.getDeclaredField("name");
        System.out.println(name);
        //获得类的方法
        System.out.println("获得类的方法=========================");
        Method[] methods = c1.getMethods();//获得本类和父类的所有public方法
        for (Method method : methods) {
            System.out.println("正常的methods " + method);
        }
        System.out.println("=========================");
        Method[] decmethods = c1.getDeclaredMethods();//获得本类的所有方法
        for (Method method : decmethods) {
            System.out.println("decmethods " + method);
        }
        //获得指定方法
        //需要传参数的原因：存在重载，参数可找到指定的方法
        System.out.println("=========================");
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);
        //获得构造器
        System.out.println("=========================");
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("getConstructors " + constructor);
        }
        System.out.println("=========================");
        Constructor[] constructors1 = c1.getDeclaredConstructors();
        for (Constructor constructor : constructors1) {
            System.out.println("getDeclaredConstructors " + constructors1);
        }
        //获得指定的构造器
        Constructor getDeclaredConstructor = c1.getDeclaredConstructor(String.class, int.class);
        System.out.println("指定构造器" + getDeclaredConstructor);
    }
}